#!/usr/bin/perl

use strict;

use DBI;
use CGI;
use TTraker;

my $dbh;
initialize_dbi;
print_header;

my $form_ref = &parse_input;

my %FORM_DATA = %$form_ref; 

my $num = $FORM_DATA{'empNum'};
my $name = $FORM_DATA{'empName'};
my $job_id = $FORM_DATA{'empJob'};
my $loc = $FORM_DATA{'empLoc'};
my $reas = $FORM_DATA{'reason'};
my $ampm = $FORM_DATA{'ampm'};
my $hrs = $FORM_DATA{'hrs'};
my $min = $FORM_DATA{'min'};
my $log_elapsed = $FORM_DATA{'log_elapsed'};
my $job_elapsed = $FORM_DATA{'job_elapsed'};
my $log_start = $FORM_DATA{'log_start'};
my $job_start = $FORM_DATA{'job_start'};
my $log_hrs = $FORM_DATA{'log_hrs'};
my $job_hrs = $FORM_DATA{'job_hrs'};
my $dow = $FORM_DATA{'dow'};
my $month = $FORM_DATA{'month'};
my $day = $FORM_DATA{'day'};
my $year = $FORM_DATA{'year'};
my $time = $FORM_DATA{'time'};
my $nod = $FORM_DATA{'nod'};
my $date_start = $FORM_DATA{'date_start'};

my $now = get_local_time($loc);
my $today = get_today;

print_log "ADJUSTING TIME NO LOG OUT:\n";
print_log "log_elapsed, job_elapsed, log_start, job_start, log_hrs, job_hrs, dow, month, day, year, time, nod, date_start\n";
print_log "$log_elapsed, $job_elapsed, $log_start, $job_start, $log_hrs, $job_hrs, $dow, $month, $day, $year, $time, $nod, $date_start\n";

my $noh = sprintf "%.1f", $log_elapsed/60;
my ($pay_count, $new_now, $msg, $job, $id);
my @dis = ();

if ( $reas == 1 ) {
	if ( $hrs =~ /^\d+$/ && $min =~ /^\d+$/ ) {
		if ($hrs >= 0 && $hrs <= 12 && $min >= 0 & $min <= 59 ) {
			$hrs = $hrs + $ampm;
			$new_now = sprintf "$date_start %02d:%02d:00", $hrs, $min;
			my $nn_status = time_stamp_compare ($log_start, $new_now);
			if ($nn_status > 0 ) {
				add_msg_payroll_hrs ($num, $date_start, $reas);
				log_out_of_work ($num, $new_now);
				log_employee_in ($num, $now);
#				normally there is not an open job when we are logged out EXCEPT for LUNCH!
				pause_current_job ($num, $now) ;
#				create an idle event to fill in the time from log_in till activity starts
				create_idle_event ($num, $now);
#				find out if the employee is logged in for payroll hours again (hopefully 0 or 1!) 
    			$pay_count = get_payroll_count ($num, $today);
#				if no payroll entry for today exists create it.
    			create_payroll_event ($num, $today) if ($pay_count == 0);
				if ( $job_id eq "CSR" ) {
					clear_hold_jobs ($num);
					csr_activity_screen_from_pm($num, $name, $job_id, $loc, "");
				} else {
					activity_screen_from_pm($num, $name, $job_id, $loc, "");
				}
			} else {
				$msg = "Can not log out earlier than you logged in!<br/>";
				redraw_log_in_status_issue_msg ($num, $name, $job_id, $loc, $log_elapsed, $job_elapsed, $log_start, $job_start, $log_hrs, $job_hrs, $dow, $month, $day, $year, $time, $nod, $date_start);
			}								
		} else { 
			$msg = "Hours must be in Range: 0-12<br/>Minutes must be in range 0-59<br/>";
				redraw_log_in_status_issue_msg ($num, $name, $job_id, $loc, $log_elapsed, $job_elapsed, $log_start, $job_start, $log_hrs, $job_hrs, $dow, $month, $day, $year, $time, $nod, $date_start);
		}
	} else {
		$msg = "Hours must be entered Range: 0-12<br/>Minutes must be in range 0-59<br/>"; 
				redraw_log_in_status_issue_msg ($num, $name, $job_id, $loc, $log_elapsed, $job_elapsed, $log_start, $job_start, $log_hrs, $job_hrs, $dow, $month, $day, $year, $time, $nod, $date_start);
	}			
} elsif ($reas == 2) {
	add_msg_payroll_hrs ($num, $date_start, $reas);
	if ( $job_id eq "CSR" ) {
		csr_activity_screen_from_pm($num, $name, $job_id, $loc, "");
	} else {
		activity_screen_from_pm($num, $name, $job_id, $loc, "");
	}
}

# print "NUM: $num, NAME: $name, JOB: $job_id, LOC: $loc, REASON: $reas, AM/PM: $ampm, HRS: $hrs, MIN: $min<br/>";

db_disconnect();

##################### SUBROUTINES FOLLOW  #####################

sub redraw_log_in_status_issue_msg
{
my $num = "$_[0]";
my $name = "$_[1]";
my $job_id = "$_[2]";
my $loc = "$_[3]";
my $log_elapsed = "$_[4]";
my $job_elapsed = "$_[5]";
my $log_start = "$_[6]";
my $job_start = "$_[7]";
my $log_hrs = "$_[8]";
my $job_hrs = "$_[9]";
my $dow = "$_[10]";
my $month = "$_[11]";
my $day = "$_[12]";
my $year = "$_[13]";
my $time = "$_[14]";
my $nod = "$_[15]";
my $date_start = "$_[16]";

#	print "redraw_log_in_status_issue_msg ($num, $name, $job_id, $loc, $log_elapsed, $job_elapsed, $log_start, $job_start, $log_hrs, $job_hrs, $dow, $month, $day, $year, $time, $nod, $date_start)<br/>";


print <<ENDHEADER;
<html>
<head>
<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
<script type='text/javascript' src='${js_ref}trak.js'></script>
<title>trak_02_activity_select</title>
</head>
<body>
<h2><center>${banner}</center></h2>
<hr/>
<div id='box1_4'>
Employee Number: <br/><br/>
Employee Name: <br/><br/>

</div>
<form id='box2_2' autocomplete='off' name='activity' action='${cgi_ref}trak_02_re_log_in_time_issue.pl' method='post'>
	<input type="hidden" name=empNum value='$num'/>
	<input type="hidden" name=empName value='$name'/>
	<input type="hidden" name=empJob value='$job_id'/>
	<input type="hidden" name=empLoc value='$loc'/>
	<input type="hidden" name=log_elapsed value='$log_elapsed'/>
	<input type="hidden" name=job_elapsed value='$job_elapsed'/>
	<input type="hidden" name=log_start value='$log_start'/>
	<input type="hidden" name=job_start value='$job_start'/>
	<input type="hidden" name=log_hrs value='$log_hrs'/>	
	<input type="hidden" name=job_hrs value='$job_hrs'/>
	<input type="hidden" name=dow value='$dow'/>
	<input type="hidden" name=month value='$month'/>
	<input type="hidden" name=day value='$day'/>
	<input type="hidden" name=year value='$year'/>
	<input type="hidden" name=time value='$time'/>
	<input type="hidden" name=nod value='$nod'/>
	<input type="hidden" name=date_start value='$date_start'/>
	<span>$num</span><br/><br/>
	<span>$name</span></b><br/><br/>
	<b id="warn">
		You have been logged in since<br/>
		$dow $month $day, $year @ $time<br/>
		That is over $noh hours! What is the reason?<br/><br/>
	</b>
	<label><input type="radio" name="reason" value="1" checked="checked" />Forgot to log out - left work at<label/>
	<label><input type="text" size="2" name="hrs"/></label>
	<label><input type="text" size="2" name="min"/></label>
	<label><input type="radio" name="ampm" value="0" />am</label>
	<label><input type="radio" name="ampm" value="12" CHECKED />pm on $dow.</label>
	<br/><br/>
	<label><input type="radio" name="reason" value="2" />&nbsp I have been working continuously during this time.</label>
	<br/>
	<br/>
	<input type='submit' value='Log Activity'>
	<br/><br/>
	<a href="${cgi_ref}trak_00_start_up.pl">Return to Log-In</a>
	<br/><br/>
	<b id="warn">
		$msg
	</b>
</form>
</body>
</html>

ENDHEADER
		
}


sub trak_02_activity_selection
{

my $msg = get_message ($num);

my $dis_ref = get_disabled_list ($num);
my @dis = @$dis_ref;

my $col_ref = get_dimmed_list ($num);
my @col = @$col_ref;

print <<ENDHEADER;
<html>
<head>
<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
<script type='text/javascript' src='${js_ref}trak.js'></script>
<title>trak_02_activity_select</title>
</head>
<body onload='startTime()'>
<h2><center>${banner}</center></h2>
<hr/>
<div id='clock'></div>
<div id='box1'>
Employee Number: <br/><br/>
Employee Name: <br/><br/>
Activity:
</div>
<form id='box2' autocomplete='off' name='activity' action='${cgi_ref}trak_02_activity.pl' method='post'>
<input type="hidden" name=empNum value='$num'/>
<input type="hidden" name=empName value='$name'/>
<input type="hidden" name=empJob value='$job'/>
<span>$num</span><br/><br/>
<span>$name</span></b><br/><br/>
<input type="radio" name="activity" value="1" checked="checked"/>&nbsp<$col[1]> Start Job</$col[1]><br/>
<input $dis[2] type="radio" name="activity" value="2"/>&nbsp<$col[2]> Finish Job</$col[2]><br/>
<input $dis[3] type="radio" name="activity" value="3"/>&nbsp<$col[3]> Log Tasks</$col[3]><br/>
<input $dis[4] type="radio" name="activity" value="4"/>&nbsp<$col[4]> Continue Job</$col[4]><br/>
<input $dis[5] type="radio" name="activity" value="5"/>&nbsp<$col[5]> Lunch</$col[5]><br/>
<input $dis[6] type="radio" name="activity" value="6"/>&nbsp<$col[6]> Break</$col[6]><br/>
<input type="radio" name="activity" value="7"/><$col[7]>&nbsp Leave Work</$col[7]><br/>
<br/>
<input type='submit' value='Log Activity'>
<br/><br/>
<a href="${cgi_ref}trak_00_start_up.pl">Return to Log-In</a>
<br/><br/>
<b id="info">$msg</b>
</form>
</body>
</html>

ENDHEADER
}

sub trak_02_sup_activity_selection
{

my $msg = & get_message ($num);
# my @dis = & get_disabled_list ($num);
# my @col = & get_dimmed_list ($num);
my @dis = ("", "", "disabled=\"disabled\"", "disabled=\"disabled\"", "disabled=\"disabled\"", "disabled=\"disabled\"", "disabled=\"disabled\"", "disabled=\"disabled\"","disabled=\"disabled\"");
my @col = ("dis","en","dis","dis","dis","dis","dis","dis"); 

print <<ENDHEADER;
<html>
<head>
<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
<script type='text/javascript' src='${js_ref}trak.js'></script>
<title>trak_02_activity_select</title>
</head>
<body onload='startTime()'>
<h2><center>${banner}</center></h2>
<hr/>
<div id='clock'></div>
<div id='box1'>
Employee Number: <br/><br/>
Employee Name: <br/><br/>
Activity:
</div>
<form id='box2' autocomplete='off' name='activity' action='${cgi_ref}trak_02_sup_activity.pl' method='post'>
<input type="hidden" name=empNum value='$num'/>
<input type="hidden" name=empName value='$name'/>
<input type="hidden" name=empJob value='$job'/>
<input type="hidden" name=empLoc value='$loc'/>
<span>$num</span><br/><br/>
<span>$name</span></b><br/><br/>
<input type="radio" name="activity" value="1" checked="checked"/>&nbsp<$col[1]> Payroll Confirmation</$col[1]><br/>
<input $dis[2] type="radio" name="activity" value="2"/>&nbsp<$col[2]> TBA</$col[2]><br/>
<input $dis[3] type="radio" name="activity" value="3"/>&nbsp<$col[3]> TBA</$col[3]><br/>
<input $dis[4] type="radio" name="activity" value="4"/>&nbsp<$col[4]> TBA</$col[4]><br/>
<input $dis[5] type="radio" name="activity" value="5"/>&nbsp<$col[5]> TBA</$col[5]><br/>
<input $dis[6] type="radio" name="activity" value="6"/>&nbsp<$col[6]> TBA</$col[6]><br/>
<input $dis[6] type="radio" name="activity" value="7"/>&nbsp<$col[7]> TBA<br/>
<br/>
<input type='submit' value='Proceed'>
<br/><br/>
<a href="${cgi_ref}trak_00_start_up.pl">Return to Log-In</a>
<br/><br/>
<b id="info">$msg</b>
</form>
</body>
</html>

ENDHEADER
}

sub trak_02_adm_activity_selection
{

my $msg = & get_message ($num);
# my @dis = & get_disabled_list ($num);
# my @col = & get_dimmed_list ($num);
my @dis = ("", "", "", "", "", "", "disabled=\"disabled\"", "disabled=\"disabled\"","disabled=\"disabled\"");
my @col = ("dis","en","en","en","en","en","dis","dis"); 

print <<ENDHEADER;
<html>
<head>
<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
<script type='text/javascript' src='${js_ref}trak.js'></script>
<title>trak_02_activity_select</title>
</head>
<body onload='startTime()'>
<h2><center>${banner}</center></h2>
<hr/>
<div id='clock'></div>
<div id='box1'>
Employee Number: <br/><br/>
Employee Name: <br/><br/>
Activity:
</div>
<form id='box2' autocomplete='off' name='activity' action='${cgi_ref}trak_02_adm_activity.pl' method='post'>
<input type="hidden" name=empNum value='$num'/>
<input type="hidden" name=empName value='$name'/>
<input type="hidden" name=empJob value='$job'/>
<input type="hidden" name=empLoc value='$loc'/>
<span>$num</span><br/><br/>
<span>$name</span></b><br/><br/>
<input type="radio" name="activity" value="1" checked="checked"/>&nbsp<$col[1]> Adjust Client Standard Time</$col[1]><br/>
<input $dis[2] type="radio" name="activity" value="2"/>&nbsp<$col[2]> Adjust Master Standard Time</$col[2]><br/>
<input $dis[3] type="radio" name="activity" value="3"/>&nbsp<$col[3]> Add New Client</$col[3]><br/>
<input $dis[4] type="radio" name="activity" value="4"/>&nbsp<$col[4]> Add Client Task</$col[4]><br/>
<input $dis[5] type="radio" name="activity" value="5"/>&nbsp<$col[5]> Add Master Task</$col[5]><br/>
<input $dis[6] type="radio" name="activity" value="6"/>&nbsp<$col[6]> TBA</$col[6]><br/>
<input $dis[6] type="radio" name="activity" value="7"/>&nbsp<$col[7]> TBA<br/>
<br/>
<input type='submit' value='Proceed'>
<br/><br/>
<a href="${cgi_ref}trak_00_start_up.pl">Return to Log-In</a>
<br/><br/>
<b id="info">$msg</b>
</form>
</body>
</html>

ENDHEADER
}


